package com.baidu.service.impl;

import com.alibaba.fastjson.JSON;
import com.baidu.model.entity.CommentModel;
import com.baidu.response.ResponseBody;
import com.baidu.service.CommentControllerService;
import com.until.pagination.PaginationRequest;
import com.until.response.RestfulCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author user
 */
@Service
public class CommentControllerImpl implements CommentControllerService {
    @Autowired
    private CommentServiceImpl commentServiceImpl;

    @Autowired
    private RestfulCode restfulCode;

    @Autowired
    private ResponseBody responseBody;

    @Override
    public RestfulCode.CodeSatCode createComment(CommentModel commentModel) {
        RestfulCode.CodeSatCode userActionFailed = null;
        try {
            int successfully=commentServiceImpl.createComment(commentModel);
            userActionFailed=successfully>0?responseBody.USER_OPERATION_SUCCEEDED:responseBody.USER_ACTION_FAILED;
        } catch (Exception e) {
            userActionFailed=responseBody.USER_INFO_EXCEPTION;
        }
        return restfulCode.getFreeResult(userActionFailed);
    }

    @Override
    public RestfulCode.CodeSatCode selectByPhotoId(Integer id, PaginationRequest page, Integer commentId) {
        RestfulCode.CodeSatCode userActionFailed =responseBody.USER_OPERATION_SUCCEEDED;
        userActionFailed.setData(JSON.toJSON(commentServiceImpl.selectByPhotoId(id,page,commentId)));
        return restfulCode.getFreeResult(userActionFailed);
    }

    @Override
    public RestfulCode.CodeSatCode deleteByPhotoId(Integer id) {
        RestfulCode.CodeSatCode userActionFailed = null;
        try {
            int successfully=commentServiceImpl.deleteByPhotoId(id);
            userActionFailed=successfully>0?responseBody.USER_OPERATION_SUCCEEDED:responseBody.NOT_FOUND_EXAMPLE;
        } catch (Exception e) {
            userActionFailed=responseBody.USER_INFO_EXCEPTION;
        }
        return restfulCode.getFreeResult(userActionFailed);
    }

}
